package com.etoak.commons.listener;

import org.springframework.context.annotation.Configuration;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Configuration
public class ETSessionListener implements HttpSessionListener {
    private static final Map<String, HttpSession> DB = new ConcurrentHashMap<>();
    @Override
    public void sessionCreated(HttpSessionEvent hse) {
        //吧session对象存起来
        HttpSession session = hse.getSession();
        DB.put(session.getId(),session);

    }

    @Override
    public void sessionDestroyed(HttpSessionEvent hse) {
        DB.remove(hse.getSession().getId());
    }
    //对外提供根据id获得session的方法
    public static HttpSession getSessionById(String sessionid){
        return DB.get(sessionid);
    }
    /*javaWeb三大核心

    * servlet  接受请求  处理请求  生成响应
    * filter   接受请求  过滤请求
    * listener  监听一些servlet中不能自己new的  来监听他们什么时候被创建  还有一些属性
    *
    *
    * */
}
